/***************************************************************************
 *   Copyright (C) 2008 by JBox systems
 *   gabi@shaargiora.co.il
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 ***************************************************************************/

#pragma once

#include <QTextEdit>
#include <QFileSystemWatcher>

#include <fstream>
#include <string>

#include "memory.h"

/**
	Tail file in document.

	Update document on changes in file.
*/
class TailWatch : public QObject {
	Q_OBJECT

	public:
		TailWatch(std::string filename, QTextEdit *document);
		~TailWatch();

		void setFile(std::string filename) { m_filename = filename; }
		void setDocument(QTextEdit *document) { m_document = document; }

	private slots:
		void updateDocument();

	private:
		std::string m_filename;
		std::ifstream *m_file;
		QTextEdit *m_document;
		QFileSystemWatcher *m_watcher;
		long pos;

		std::string filename() const { return m_filename; }
		QTextEdit* document() const { return m_document; }
};
